﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WSweb
{
    public partial class TEST : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Header.Title = "WSweb - Novosti";

        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item;
            Literal lit_naziv = (Literal)item.FindControl("lit_naziv");
            TextBox fld_naziv = (TextBox)item.FindControl("fld_naziv");
            Literal lit_sadrzaj = (Literal)item.FindControl("lit_sadrzaj");
            TextBox fld_sadrzaj = (TextBox)item.FindControl("fld_sadrzaj");
            LinkButton lnk_Edit = (LinkButton)item.FindControl("lnk_Edit");
            LinkButton lnk_Cancel = (LinkButton)item.FindControl("lnk_Cancel");
            Literal id_vijest = (Literal)item.FindControl("lit_id_vijest");
            if ((string)Session["korisnik"] != "admin")
                Response.Redirect("Nedozvoljena_autorizacija.aspx");
            if (e.CommandName == "EditThis")
            {
                lit_naziv.Visible = false;
                fld_naziv.Visible = true;
                lit_sadrzaj.Visible = false;
                fld_sadrzaj.Visible = true;
                lnk_Edit.Text = "Spremi";
                lnk_Edit.CommandName = "SaveChanges";
                lnk_Cancel.Visible = true;
                fld_sadrzaj.TextMode = TextBoxMode.MultiLine;
                fld_naziv.Width = 400;
                fld_sadrzaj.Width = 500;
                fld_sadrzaj.Height = 200;
                fld_sadrzaj.Wrap = true;
            }
            else if (e.CommandName == "CancelEdit")
            {
                lit_naziv.Visible = true;
                fld_naziv.Visible = false;
                lit_sadrzaj.Visible = true;
                fld_sadrzaj.Visible = false;
                lnk_Edit.Text = "Uredi";
                lnk_Cancel.Visible = false;
            }
            else if (e.CommandName == "SaveChanges")
            {
                SqlConnection c = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DBwswebCS"].ConnectionString);
                c.Open();
                SqlCommand comm = new SqlCommand("update Vijest set naziv=@naziv, sadrzaj=@sadrzaj where id_vijest=@id_vijest", c);
                comm.Parameters.AddWithValue("@naziv", fld_naziv.Text);
                comm.Parameters.AddWithValue("@sadrzaj",fld_sadrzaj.Text);
                comm.Parameters.AddWithValue("@id_vijest",id_vijest.Text);
                comm.ExecuteNonQuery();
                c.Close();
                lnk_Edit.CommandName = "EditThis";
                Repeater1.DataBind();
                
            }
        }
    }
}